package book;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.util.List;
import java.util.Vector;

import javax.swing.*;

import bookshopSM.BookshopGUI;
import bookshopSM.DrawPanel;

import sale.Sale;
import sale.SaleManager;

public class SelectBook extends JPanel{
    private JLabel ltitle,lname,lpress,lauthor,lsort,ltitlel,ltitler;
    private JTextField tfname,tfpress,tfauthor,tfsort;
    JButton bbegin;
    JComboBox<String> csort;
    private JPanel p,pbig;
    private Vector rowData, columnName;
    private JTable jt = null;
    private JScrollPane jsp = null;
    private JLabel btitle,btitlebig;
    Box box2;
    private BookshopGUI userInterface2;
    private JButton  firstButton1,secondButton1;
    private JLabel label;
    int vnoDelete;
    static  Connection conn = null;
    private static String username ="root";
    private static String password="root" ;
    private static String driverClassName="com.mysql.jdbc.Driver";
    private static String url="jdbc:mysql://127.0.0.1:3307/books_system";
    String picturePath;
    private DrawPanel myPanel;

    public SelectBook(){
        p=new JPanel();
        pbig=new JPanel();
        p.setLayout(new BorderLayout());
        pbig.setLayout(new BorderLayout());
        columnName = new Vector();
        //设置列名
        columnName.add("图书编号");
        columnName.add("书名");
        columnName.add("出版社");
        columnName.add("作者");
        columnName.add("分类");
        ltitler=new JLabel();
        ltitler.setFont(new java.awt.Font("楷体",1,14));
        ltitle=new JLabel("图书查询");
        ltitle.setFont(new java.awt.Font("楷体",1,20));
        ltitlel=new JLabel("查看详细信息     ");
        ltitlel.setFont(new java.awt.Font("楷体",1,14));
        lname=new JLabel("书名");
        lpress=new JLabel("出版社");
        lauthor=new JLabel("作者");
        lsort=new JLabel("分类");
        tfname=new JTextField(4);
        tfpress=new JTextField(4);
        tfauthor=new JTextField(4);
        tfsort=new JTextField(4);
        bbegin=new JButton("查找");
        csort=new JComboBox<String>();
        csort.setModel(new DefaultComboBoxModel<String>(new String[] {"教育","文艺","文学","童书","人文社科","经济","科学技术"}));

        picturePath="bgp.jpg";
        myPanel=new DrawPanel();
        String names2[]={ "图书编号","书     名","剩余库存","分      类","定 价(元)","售 价(元)","作     者", "出 版 社"};
        userInterface2 = new BookshopGUI(names2 );
        label=new JLabel("              图书封面                             ");
        userInterface2.hideDoTask1Button();
        userInterface2.hideDoTask2Button();

        Box box = Box.createVerticalBox();
        Box boxinner=Box.createHorizontalBox();
        Box boxinnerin=Box.createVerticalBox();
        boxinnerin.add(myPanel);
        boxinnerin.add(label);
        boxinner.add(userInterface2);
        boxinner.add(boxinnerin);
        box.add(Box.createVerticalStrut(65));
        box.add(ltitlel);
        box.add(Box.createVerticalStrut(15));
        box.add(boxinner );
        box.add(Box.createVerticalStrut(65));


        Box box1=Box.createHorizontalBox();
        box1.add(lname);
        box1.add(tfname);
        box1.add(lpress);
        box1.add(tfpress);
        box1.add(lauthor);
        box1.add(tfauthor);
        box1.add(lsort);
        box1.add(tfsort);
        box1.add(csort);csort.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        // TODO Auto-generated method stub
                        tfsort.setText((String) csort.getSelectedItem());//设置文本框的内容
                    }//设置事件响应器

                });
        box1.add(bbegin);
        bbegin.addActionListener(
                new ActionListener() {
                    public void actionPerformed( ActionEvent e )
                    {
                        try {
                            DisplayRecord();
                        } catch (SQLException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                    }});
        p.add(ltitler,BorderLayout.NORTH);
        pbig.add(box1,BorderLayout.NORTH);
        pbig.add(p,BorderLayout.CENTER);;
        Box box3=Box.createHorizontalBox();
        box3.add(box);
        box3.add(pbig);
        Box box4=Box.createVerticalBox();
        box4.add(Box.createVerticalStrut(15));
        box4.add(ltitle);
        box4.add(Box.createVerticalStrut(15));
        box4.add(box3);
        this.add(box4);
    }

    protected void DisplayRecord() throws SQLException {
        // TODO Auto-generated method stub

        rowData = new Vector();
        String sqlString=new String("select * " +
                " from Book " +
                " where Bname like '%"+tfname.getText()+"%'"+
                " and Bpress like '%"+tfpress.getText()+"%'"+
                " and Bauthor like '%"+tfauthor.getText()+"%'"+
                " and Bsort like '%"+tfsort.getText()+"%'");
        // System.out.print(sqlString);
        BookManager bookmanager=new  BookManager();
        List<Book> list=bookmanager.Query(sqlString);
        System.out.print(list.size());
        for (int i=0;i<list.size();i++){
            Vector<String> line1 = new Vector<String>();
            line1.add(String.valueOf(list.get(i).getBno()));
            line1.add(list.get(i).getBname());
            line1.add(list.get(i).getBpress());
            line1.add(list.get(i).getBauthor());
            line1.add(list.get(i).getBsort());
            rowData.add(line1);
        }
        //System.out.print("aa");
        ltitler.setText("查找结果");
        jt = new JTable(rowData, columnName);
        jsp = new JScrollPane(jt);
        p.removeAll();
        p.add(ltitler,BorderLayout.NORTH);
        p.add(jsp,BorderLayout.CENTER);
        jt.addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent event)
            {

                int row = jt.rowAtPoint(event.getPoint());
                Object selected = jt.getModel().getValueAt(row,0);
                vnoDelete=Integer.valueOf((String) selected);
                //jt.setRowSelectionInterval(row, row);
                try {
                    DisplayRecord1();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }

    private void DisplayRecord1() throws SQLException {
        // TODO Auto-generated method stub
        conn = DriverManager.getConnection(url,username,password);
        Statement stmt=conn.createStatement();
        //vnoDelete=Integer.valueOf(fieldValues1[0]);
        try {
            //System.out.print(vnoDelete);
            ResultSet rs = stmt.executeQuery("select * from Book where Bno="+vnoDelete);
            rs.next();//rs指向第一行之前
            // "书     名","剩余库存","分     类","定     价(元)","作     者", "出 版 社"
            userInterface2.fields[0].setText(String.valueOf(vnoDelete));
            userInterface2.fields[1].setText(rs.getString("Bname"));
            userInterface2.fields[2].setText(String.valueOf(rs.getInt("Bnum")));
            userInterface2.fields[3].setText(rs.getString("Bsort"));
            userInterface2.fields[4].setText(String.valueOf(rs.getFloat("Bprice")));
            userInterface2.fields[5].setText(String.valueOf(rs.getFloat("Bsellprice")));
            userInterface2.fields[6].setText(rs.getString("Bauthor"));
            userInterface2.fields[7].setText(rs.getString("Bpress"));
            picturePath=rs.getString("Bcover");
            myPanel.setimg(picturePath);
            myPanel.draw();
        }

        catch (Exception ee){
            JOptionPane.showMessageDialog(null, "无相关记录");
            System.out.println(ee);
        }
        finally{
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                conn=null;
            }
        }

    }
}
